﻿using System;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;
using DevExpress.Web.ASPxUploadControl;
using System.IO;
using System.Text;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 数据恢复
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class FwmmsjhfController : AdminBaseController
    {
        //
        // GET: /Admin/Fwmmsjhf/
        /// <summary>
        /// 数据恢复主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_Fwmmsjhf", Order = 1)]
        [LogPageActionFilter("数据恢复", Order = 2)]
        public ActionResult Index()
        {
            BLL.Rc rcBll = new BLL.Rc();
            Model.Rc rcModel;

            rcModel = rcBll.GetModel("imp_date");
            if (rcModel == null)
            {
                rcModel = new Model.Rc();
                rcModel.value = "";
            
            }


            return View(rcModel);
        }


        /// <summary>
        /// 数据恢复
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult SaveFwmmsjhf() {
            ActionResult actionResult = null;
            string backname = string.Empty;
            string svrFileName = string.Empty;
            string uploadoffiepath = string.Empty;
            BLL.Rc rcBll = new BLL.Rc();
            Model.Rc rcModel = new Model.Rc();
            try
            {
                UploadedFile[] uploadFiles = UploadControlExtension.GetUploadedFiles("upload");
                if (uploadFiles.Length > 0 && null != uploadFiles)
                {
                    for (int i = 0; i < uploadFiles.Length; i++)
                    {
                    UploadedFile upload = uploadFiles[i];
                    if (upload != null)
                        {
                            backname = upload.FileName.Substring(upload.FileName.LastIndexOf('.') + 1);

                            string entityNo = GlobalConstant.ENTITYNO;

                            string dest_dir = "/Publish/" + entityNo + "/sql/" + DateTime.Now.Year + DateTime.Now.Month + "/";

                            svrFileName = "data";

                            string oldfilename = upload.FileName.Substring(upload.FileName.LastIndexOf('\\') + 1);

                            uploadoffiepath = Server.MapPath(Url.Content("~") + dest_dir);//上传文件的路径

                            if (!Directory.Exists(uploadoffiepath))//如果不存在就创建
                            {
                                Directory.CreateDirectory(uploadoffiepath);
                            }

                            upload.SaveAs(uploadoffiepath + svrFileName + "." + backname);

                            
                            ViewData["message"] = "文件上传保存成功";
                            ViewData["IsClose"] = "Y";
                            actionResult = View("Index", rcModel);
                            
                        }
                    }
                }            

            }
            catch(Exception) {
                ViewData["message"] = "文件上传保存失败";
                ViewData["IsClose"] = "N";
                actionResult = View("Index", rcModel);
            }

            Zip.UnZip(uploadoffiepath + svrFileName + "." + backname, uploadoffiepath + svrFileName + ".sql");

            FileStream file = new FileStream(uploadoffiepath + svrFileName + ".sql", FileMode.OpenOrCreate);
            StreamReader reader = new StreamReader(file, System.Text.Encoding.GetEncoding("GB2312"));
            
            string str = "";
            string strarr = "";
            StringBuilder builer = new StringBuilder();
            while ((str = reader.ReadLine()) != null)
            {
                str = str.Replace("nc_fwmmyh", "dbo.nc_fwmmyh");
                //str = str.Replace("nc_fwmmyhsr", "dbo.nc_fwmmyhsr");//上面已经替换了
                str = str.Replace("nc_fwmmszbt", "dbo.nc_fwmmszbt");
                //strarr += str;
                builer.AppendLine(str);
            }

            reader.Close();
            file.Close();
            strarr = builer.ToString();
            BLL.Rc fwmmyhsrBll = new BLL.Rc();

            fwmmyhsrBll.Recover(strarr, 6000000);
            if (!rcBll.Exists("imp_date"))
            {
                rcModel.name = "imp_date";
                rcModel.value = DateTime.Now.ToString();
                rcModel.memo = "数据恢复时间";
                rcBll.Add(rcModel);

            }
            else
            {
                rcModel.name = "imp_date";
                rcModel.value = DateTime.Now.ToString();
                rcBll.Update(rcModel);
            }
                            


            ViewData["message"] = "恢复数据成功";
            ViewData["IsClose"] = "Y";
            actionResult = View("Index", rcModel);

            return actionResult;
        }


    }
}
